package com.courseware.online.service;

import com.courseware.online.common.BusinessException;
import com.courseware.online.dao.UserDao;
import com.courseware.online.dto.UserDTO;
import com.courseware.online.entity.User;
import com.courseware.online.vo.UserVO;
import org.apache.commons.lang3.StringUtils;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author demos
 */
@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private UserDao userDao;
    @Autowired
    private Mapper dozerMapper;

    @Override
    public UserVO login(UserDTO dto) {
        User userParam = dozerMapper.map(dto, User.class);
        User user = userDao.getUser(userParam);
        if (user == null || StringUtils.isBlank(user.getName())) {
            throw new BusinessException(500, "用户名或密码错误");
        }
        return dozerMapper.map(user, UserVO.class);
    }

    @Override
    public UserVO update(UserDTO dto) {
        User userParam = dozerMapper.map(dto, User.class);
        userDao.updateById(userParam);
        User user = userDao.getUser(userParam);
        return dozerMapper.map(user, UserVO.class);
    }

    @Override
    public UserVO Register(UserDTO user) {
        User userParam = dozerMapper.map(user, User.class);
        userDao.createUser(userParam);
        return dozerMapper.map(userDao.getUser(userParam), UserVO.class);
    }
}
